<?php

class Admin_CursoController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_forward("index", "login", "admin");
        } else {
            $identity = $auth->getIdentity();
            if ($identity['tipo_usuario'] != VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_forward("index", "login", "admin");
            }
        }
        $default = Zend_Registry::get('default_messages');
        $this->view->deleteConfirmMessage = $default->delete_confirm_message;
    }

    public function preDispatch() {
        // Autenticacion del usuario
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_helper->layout->disableLayout();
            $this->_forward("index", "index", "");
        } else {
            $identity = $auth->getIdentity();
            if ($identity['tipo_usuario'] != VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_redirect('/');
            }
        }
        //fin de la autenicacion del usuario
    }

    public function indexAction() {
        
    }

    public function saveAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $codigo_curso = $this->getRequest()->getParam("codigo_curso");

        $form = new Admin_Form_Curso();
        $form->populate($_POST);

        if ($form->isValid($_POST)) {
            $curso = new Application_Model_Curso();
            if ($codigo_curso) {
                $curso_db = $curso->find($codigo_curso);
                $curso_db->setNombre($form->getValue('nombre'));
                $curso_db->setFechaFin($form->getValue('fecha_fin'));
                $curso_db->setFechaInicio($form->getValue('fecha_inicio'));
                $curso_db->setCantidadHoras($form->getValue('cantidad_horas'));
                $curso_db->setCodigoAreaAcademica($form->getValue('codigo_area_academica'));
                if ($curso_db->save(true)) {
                    $this->_forward('index');
                } else {
                    $error_message = Zend_Registry::get("error_messages");
                    echo Zend_Json_Encoder::encode($error_message->insert_error);
                }
            } else {

                $curso->setNombre($form->getValue('nombre'));
                $curso->setFechaFin($form->getValue('fecha_fin'));
                $curso->setFechaInicio($form->getValue('fecha_inicio'));
                $curso->setCantidadHoras($form->getValue('cantidad_horas'));
                $curso->setCodigoAreaAcademica($form->getValue('codigo_area_academica'));

                if ($curso->save()) {
                    $this->_forward('index');
                } else {
                    $error_message = Zend_Registry::get("error_messages");
                    echo Zend_Json_Encoder::encode($error_message->insert_error);
                }
            }
        } else {
            print_r($form->getErrors());
            die();
        }
    }

    public function editAction() {
        $codigo_curso = $this->getRequest()->getParam("id");
        $form = new Admin_Form_Curso();
        if (isset($codigo_curso)) {
            $curso = new Application_Model_Curso();
            $curso_db = $curso->find($this->getRequest()->getParam("id"));

            $form->populate($curso_db->toArray());
        }
        $this->view->form = $form;
    }

    public function viewAction() {
        $this->_deleteAction = true;
        $this->_helper->viewRenderer->setNoRender(true);
        $codigo_curso = $this->getRequest()->getParam("id");
        if (isset($codigo_curso)) {
            $curso = new Application_Model_Curso();
            $curso_db = $curso->find($this->getRequest()->getParam("id"));
            print_r($curso_db);
        }
    }

    public function listAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();

        $curso = new Application_Model_Mapper_Curso();
        $list = $curso->fetchAll();
        $response = array();
        foreach ($list as $a) {
            $row = $a->toArray();
            $area_academica = new Application_Model_AreaAcademica();
            $area_db = $area_academica->find($row['codigo_area_academica']);
            $response [] = array($this->view->deleteUrl($row['codigo_curso'], "curso"), $this->view->editUrl($row['codigo_curso'], $row['nombre'], "curso"),
                $row['fecha_inicio'], $row['fecha_fin'],
                $row['cantidad_horas'], $this->view->viewUrl($row['codigo_area_academica'], $area_db->getNombre(), "areaacademica"));
        }
        echo Zend_Json_Encoder::encode($response);
    }

    public function deleteAction() {
        $this->_deleteAction = true;
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $codigo_curso = $this->getRequest()->getParam("id");
        if (isset($codigo_curso)) {

            $curso = new Application_Model_Curso();
            $curso_db = $curso->find($codigo_curso);
            $curso_mapper = new Application_Model_Mapper_Curso();
            try {
                $curso_mapper->delete($curso_db);
                $this->_forward('index');
//                echo Zend_Json_Encoder::encode(array("tipo" => "error", "mensaje" => $this->_errorMessages->completar_operacion));
            } catch (Exception $e) {
//                echo Zend_Json_Encoder::encode(array("tipo" => "error", "mensaje" => $this->_errorMessages->completar_operacion));
            }
//            echo Zend_Json_Encoder::encode(array("tipo" => "error", "mensaje" => $this->_errorMessages->datos_relacionados));            
        } else {
//            echo Zend_Json_Encoder::encode(array("tipo" => "error", "mensaje" => $this->_errorMessages->completar_operacion));
        }
    }

}

